package cn.yunbamboos.rest.service.session;

import cn.yunbamboos.rest.service.ISession;

public abstract class SessionHelper {

    private boolean setSessionMethodCalled;
    protected ISession session;

    public void setSession(ISession session) {
        if (setSessionMethodCalled) {
            throw new IllegalStateException("setSession() method can only be called once.");
        }
        setSessionMethodCalled = true;
        this.session = session;
    }

    public String getSession(String key) {
        return this.session.get(key);
    }

    public String getSessionRequired(String key) {
        return this.session.getRequired(key);
    }

    public Integer getSessionInteger(String key) {
        return Integer.parseInt(this.session.get(key));
    }

    public Integer getSessionRequiredInteger(String key) {
        return Integer.parseInt(this.session.getRequired(key));
    }

    public Float getSessionFloat(String key) {
        return Float.parseFloat(this.session.get(key));
    }

    public Float getSessionRequiredFloat(String key) {
        return Float.parseFloat(this.session.getRequired(key));
    }

    public Double getSessionDouble(String key) {
        return Double.parseDouble(this.session.get(key));
    }

    public Double getSessionRequiredDouble(String key) {
        return Double.parseDouble(this.session.getRequired(key));
    }

    public Long getSessionLong(String key) {
        return Long.parseLong(this.session.get(key));
    }

    public Long getSessionRequiredLong(String key) {
        return Long.parseLong(this.session.getRequired(key));
    }

    public Boolean getSessionBoolean(String key) {
        return Boolean.parseBoolean(this.session.get(key));
    }

    public Boolean getSessionRequiredBoolean(String key) {
        return Boolean.parseBoolean(this.session.getRequired(key));
    }


}
